<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Pool;
use Illuminate\Http\Request;

class Jiaoyiba extends AbstractApi
{
    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $number = '';
        $faceValue = '';
        if ($bean->getGoodsId() == 10){
            $number = 1;
            $faceValue = $bean->getRechargeNum() * $bean->getStartCharging();
        }
        elseif ($bean->getGoodsId() == 13){
            $number = $bean->getRechargeNum() * $bean->getStartCharging();
            $faceValue  = 1;
        }
        $data = array2json([
            'merAccount'        => $bean->getSupplierNum(), //商户号
            'businessType'      => $bean->getGoodsId(), //商品编号id
            'merUserAccount'    => '',
            'extendAccount'     => '',
            'attach'            => '',
            'merOrderNo'        => $bean->getOrderNo(),
            'merOrderTime'      => date('Y-m-d H:i:s'),//时间
            'rechargeAccount'   => $bean->getAccount(), //要充值的用户
            'rechargeValue'     => $faceValue,//面值
            'rechargeNum'       => $number,
            'customerIp'        => $bean->getIp(),//获取用户当前的ip
            'notifyUrl'         => $bean->getNotifyUrl(), //异步回调地址
            'extendParam'       => [
                'productId'     => $bean->getProductId(), //产品id号
                'gameAccount'   => $bean->getGameAcct(), //游戏账号
                'gameAreaId'    => $bean->getGameAreaId(), //游戏区id
                'gameServerId'  => $bean->getGameServerId(), //游戏服id
                'gameAreaName'  => $bean->getGameAreaName(), //游戏区名称
                'gameServerName'=> $bean->getGameServerName(), //游戏服名称
            ]
        ]);
        $sign = md5(sprintf('data=%s&key=%s', $data, $bean->getNotifyUrl()));

        return ['data' => $data, 'sign' => $sign];
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $data = json2array($request->input('data', ''));
        $status = data_get($data, 'orderState', '');
        $order_sn = data_get($data, 'merOrderNo', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['23', '24']))->setRechargeSuccess($status == '24');
    }
}
